package resilienceManager.qmul.gemom;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.util.Date;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import cbr.resilienceManager.qmul.gemom.CbrData;
import cbr.resilienceManager.qmul.gemom.CbrSaving;
import cbr.resilienceManager.qmul.gemom.CbrSearch;


/*
 * COPYRIGHT DISCLAIMER
 * Synopsis: All the source codes presented in this document are intellectual
 * properties of Queen Mary, University of London.
 * 
 * @year 2010
 * @author Peng Jiang, John Bigham, Jiayi Wu Jinfu Wang------------QMUL
 * @version 1.0 
 */


public class AutoFrame extends JFrame {



	private Gbroker[] glist;

	private int brokenGid=0;;

	private float sv=0;

	private CbrData cd;

	private CbrSearch cs;


	public AutoFrame(Gbroker[] gblist, int bgid){

		super ("Control Panel");

		this.glist=gblist;

		this.brokenGid=bgid;

		//////////////case based reasoning/////////////////////////////////

		// delete the broken broker from the list
		SetGB(glist,brokenGid);

		cd = new CbrData(glist,brokenGid);

		cs = new CbrSearch(cd);
		
		cd = cs.getCbr();
		


		/////////////////////////////////////////////////////////////////////////////////////////		

		// create the panel for the button
		JPanel buttonPanel = new JPanel(new GridLayout(4, 4));


		// start button and action
		JButton manualButton = new JButton("Manul Control");
		manualButton.addActionListener(new java.awt.event.ActionListener(){
			public void actionPerformed(ActionEvent event) {

				NSPartitioner app = new NSPartitioner(cd);

				app.SetGB(glist, brokenGid);


				setVisible(false);

			}
		});
		buttonPanel.add(manualButton);


		// start button and action
		JButton autolButton = new JButton("Case-Based reasoning");
		autolButton.addActionListener(new java.awt.event.ActionListener(){
			public void actionPerformed(ActionEvent event) {
				NSitem[] t=Datasaver.getItems(cd.getTableID());

				AuctionFrame af=new AuctionFrame(t, glist, cd,true);


				setVisible(false);

			}
		});
		buttonPanel.add(autolButton);


		// the label to show the similarity value
		String text;
		String text2;
		if(cs.getValue()>0){
			text="The Similarity value="+ cs.getValue();
			text2="The risk value="+cd.getRisk();
		}
		else{
			text="No similar case in DB";
			text2="";
		}

		JLabel label = new JLabel(text, SwingConstants.CENTER); 
		buttonPanel.add(label);
		
		JLabel label2 = new JLabel(text2, SwingConstants.CENTER); 
		buttonPanel.add(label2);
		


		// add the button panel in the frame
		add (buttonPanel,   BorderLayout.CENTER);

		setSize(200, 200);
		Dimension scr = Toolkit.getDefaultToolkit().getScreenSize(); 
		Dimension frm = this.getSize(); 
		setLocation( (scr.width - frm.width) / 2, 
				(scr.height - frm.height) / 2 - 18); 


		setVisible(true);

	}


	/**
	 * remove the broken broker from the broker list 
	 * @return 
	 * */	

	public void SetGB(Gbroker[] gb, int bg)
	{
		this.glist=new Gbroker[gb.length-1];
		Vector<Gbroker> gtemp=new Vector<Gbroker>();
		int m=0;

		for(int i=0; i<gb.length; i++)
		{
			if(gb[i].getID()!=bg)
			{
				gtemp.add(gb[i]);
				this.glist[m]=gb[i];
				m++;
			}
		}


		this.brokenGid=bg;
	}



}
